<script setup lang="ts">
import {onMounted, reactive, ref} from "vue";
import {convertToSeconds, msg} from "@/config/Tool";
import {visitPath} from "@/api/edu/fileApi";
import {postUpdateSpeed} from "@/api/edu/speedApi";
const props=defineProps(['item','palyIndex','itemIndex']);
let modelDateItem ='';
const indexNum =ref(0);
let playerOptions = reactive({
  // height: 200,
  // width: document.documentElement.clientWidth, //播放器宽度
  playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
  autoplay: false, // 如果true,浏览器准备好时开始回放。
  muted: false, // 默认情况下将会消除任何音频。
  loop: false, // 导致视频一结束就重新开始。
  preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
  language: 'zh-CN',
  aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
  fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
  notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
  controls: true,
  currentTime:0,
  controlBar: {
    timeDivider: true,
    durationDisplay: true,
    playToggle: true,
    currentTimeDisplay: true,
    // 剩余时间
    remainingTimeDisplay: true,
    // 进度条
    progressControl: false,
    fullscreenToggle: true // 全屏按钮
  }
})

const myVideo = ref(null);
const Emit = defineEmits(["receiveVideoPlayer"]);
onMounted(()=>{
  const modelDate = props.item;
  const tempValue = modelDateItem || modelDate;
  modelDateItem = tempValue;
  playerOptions.currentTime=props.item.visitLong*1
  //console.log(props.item)
})

/**
 * 播放时长改变触发
 * @param e
 */
function timeupdateFun(e:any) {
  var paragraphs = e.target.getElementsByClassName("vjs-current-time-display");
  let seconds= convertToSeconds(paragraphs[0].textContent);
  let param = props.item;
 if (param.visitLong*1<seconds){
   param.visitLong=seconds;
   param.speed=(seconds/param.totalLong)*100
 }
};
/**
 * 视频暂停触发
 */
function pauseFun(e:any){
  var paragraphs = e.target.getElementsByClassName("vjs-current-time-display");
  let seconds= convertToSeconds(paragraphs[0].textContent)
  let param = props.item;
  if (param.visitLong*1<=seconds){
    param.visitLong=seconds.toString();
    param.speed=((param.visitLong/param.totalLong)*100).toString()
    postUpdateSpeed(param).then(res=>{
      if (res.code==200){Emit('receiveVideoPlayer',false)}
    })
  }
}



/**
 * 结束播放触发
 * @param e
 */
function endedFun(e:any){
  let param = props.item;
  if (param.speed==100){
    param.visitLong=param.totalLong;
    param.speed="100"
    postUpdateSpeed(param).then(res=>{
      if (res.code==200){Emit('receiveVideoPlayer',false)}
    })
  }
}




</script>

<template>
  <div>
    <video-player
        @pause="pauseFun"
        @ended="endedFun"
        id="myVideo"
        :ref="myVideo"
        @timeupdate="timeupdateFun"
        :src="visitPath+item.videoUrl"
        :options="playerOptions"
        :volume="0.6"

    />
  </div>
</template>

<style scoped>

</style>
